package com.cyufeng.blog.repository;

import com.cyufeng.blog.dto.ArticleDto;
import com.cyufeng.blog.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 文章
 *
 * @author Cyufeng
 * Data:2019.01.05
 */
@Repository
public interface ArticleRepository extends JpaRepository<Article, String>, JpaSpecificationExecutor<Article> {
    /**
     * 查询所有的文章
     *
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_ENABLE='1' and t.IS_TOOL='0' ORDER BY MADE_TIME DESC", nativeQuery = true)
    List<Article> getAllArticle();

    /**
     * 查询所有文章，并且分页
     *
     * @param pageable
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.IS_TOOL = '0' ORDER BY MADE_TIME DESC", countQuery = "select count(*) from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.IS_TOOL = '0' ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Article> getAllArticle(Pageable pageable);

    /**
     * 有条件查询所有文章，并且分页
     *
     * @param pageable
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.IS_TOOL = '0' and t.CLASS = ? ORDER BY MADE_TIME DESC", countQuery = "select count(*) from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.IS_TOOL = '0' and t.CLASS = ? ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Article> getAllArticle(int sort, Pageable pageable);

    /**
     * 根据id查找文章
     *
     * @param id
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.ID = ?1 and t.IS_TOOL = '0'", nativeQuery = true)
    Article findOneArticleById(int id);

    /**
     * 有条件查询所有工具，并且分页
     *
     * @param id
     * @param pageable
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.IS_TOOL = '1' ORDER BY MADE_TIME DESC", countQuery = "select count(*) from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.IS_TOOL = '1' ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Article> getAllTool(int id, Pageable pageable);

    /**
     * 根据id查找工具
     *
     * @param id
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_ENABLE = '1' and t.ID = ?1 and t.IS_TOOL = '1'", nativeQuery = true)
    Article findOneToolById(int id);


    /**
     * 更新是否启用字段
     *
     * @return
     */
    @Modifying
    @Query(value = "update article t set IS_ENABLE = ? where t.DR ='0' and t.id =?", nativeQuery = true)
    void setIsEnable(int isEnable, int id);

    /**
     * 有条件查询所有文章，并且分页--用于后台操作
     *
     * @param pageable
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0'  and t.IS_TOOL = '0' and t.CLASS = ? ORDER BY MADE_TIME DESC", countQuery = "select count(*) from article t where t.DR ='0' and t.IS_TOOL = '0' and t.CLASS = ? ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Article> getAllArticle_back(int sort, Pageable pageable);

    /**
     * 查询所有文章，并且分页--用于后台操作
     *
     * @param pageable
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0'  and t.IS_TOOL = '0' ORDER BY MADE_TIME DESC", countQuery = "select count(*) from article t where t.DR ='0' and t.IS_TOOL = '0' ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Article> getAllArticle_back(Pageable pageable);

    /**
     * 删除用户
     *
     * @return
     */
    @Modifying
    @Query(value = "update article t set t.DR = 1 where t.id =?", nativeQuery = true)
    void deleteArticle(int id);

    /**
     * 有条件查询所有工具，并且分页--用于后台操作
     *
     * @param id
     * @param pageable
     * @return
     */
    @Query(value = "select * from article t where t.DR ='0' and t.IS_TOOL = '1' ORDER BY MADE_TIME DESC", countQuery = "select count(*) from article t where t.DR ='0' and t.IS_TOOL = '1' ORDER BY MADE_TIME DESC", nativeQuery = true)
    Page<Article> getAllTool_back(int id, Pageable pageable);
}
